package com.foin.city.widget.navigation;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.foin.city.R;


/**
 * @author Foin
 * @version 1.0
 * @time 2017/4/19
 * @desc 实现导航的基类
 */
public abstract class AbsNavigationBar<P extends AbsNavigationBar.Builder.AbsNavigationBarParams> implements INavigationBar {

    private P absParams;

    protected View navigationView;

    public AbsNavigationBar(P absParams) {
        this.absParams = absParams;
        createView();
    }

    public P getAbsParams() {
        return absParams;
    }

    /**
     * 创建绑定View
     */
    private void createView() {
        // 1. 创建View
        if (absParams.parent == null) {
            // 获取activity的根布局，View源码
            ViewGroup activityRoot = (ViewGroup) ((Activity) (absParams.context))
                    .getWindow().getDecorView();
            absParams.parent = (ViewGroup) activityRoot.getChildAt(0);
        }
        // 处理Activity的源码
        if (absParams.parent == null) {
            return;
        }
        absParams.content = (ViewGroup) (navigationView = LayoutInflater.from(absParams.context).
                inflate(bindLayoutId(), absParams.parent, false));// 插件换肤
        absParams.content.setId(R.id.navigation_bar_content);
        if (absParams.parent.findViewById(R.id.navigation_bar_content) != null) {
            absParams.parent.removeViewAt(0);
        }
        // 2.添加
        absParams.parent.addView(navigationView, 0);
        applyView();
    }

    public void setImageResource(int viewId, int resId) {
        ImageView iv = findViewById(viewId);
        if (resId != 0) {
            iv.setImageDrawable(ContextCompat.getDrawable(absParams.context.getApplicationContext(), resId));
        }
    }

    public void setOnClickListener(int viewId, View.OnClickListener clickListener) {
        findViewById(viewId).setOnClickListener(clickListener);
    }

    public void setText(int viewId, String text) {
        TextView tv = findViewById(viewId);
        tv.setText(text);
    }

    public void setTextColor(int viewId, int color) {
        TextView tv = findViewById(viewId);
        tv.setTextColor(color);
    }

    /**
     * 根据ID查找View并做强转
     *
     * @param viewId
     * @param <T>
     * @return
     */
    public <T extends View> T findViewById(int viewId) {
        return (T) navigationView.findViewById(viewId);
    }

    public abstract static class Builder {

        AbsNavigationBarParams params;

        public Builder(Context context, ViewGroup parent) {
            params = new AbsNavigationBarParams(context, parent);
        }

        public abstract AbsNavigationBar builder();

        public static class AbsNavigationBarParams {

            public Context context;

            public ViewGroup parent;

            public ViewGroup content;

            public AbsNavigationBarParams(Context context, ViewGroup parent) {
                this.context = context;
                this.parent = parent;
            }
        }

    }

}
